package com.sn.system.common.validator.validators;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public class RequiredDependValidator extends FieldValidatorSupport{

	private String dependOn;
	private String dependvalue;
	public void validate(Object object) throws ValidationException {
		 String fieldName = getFieldName();
	        Object value = this.getFieldValue(fieldName, object);
	        Object deVal=this.getFieldValue(dependOn, object);
	        if (deVal == null ) {
	            return;
	        }
	        
	        // XW-375 - must be a string
	        if (!(deVal instanceof String)) {
	            return;
	        }

	        // string must not be empty
	        String str = ((String) deVal).trim();
	        if (str.length() == 0) {
	            return;
	        }
	        if (!str.equals(dependvalue)) {
	            return;
	        }
	        
	        if(value==null||value.equals("")){
	        	addFieldError(fieldName, object);
		        return;
	        }
		
	}
	public String getDependOn() {
		return dependOn;
	}
	public void setDependOn(String dependOn) {
		this.dependOn = dependOn;
	}
	public String getDependvalue() {
		return dependvalue;
	}
	public void setDependvalue(String dependvalue) {
		this.dependvalue = dependvalue;
	}


	
}
